package com.zjf.carpark.controller;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.LicensePlateOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.LicensePlateOCRResponse;

import com.zjf.carpark.utils.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.zjf.carpark.utils.CodeAndMessage.*;


/**
 * @description: 图片扫描接口
 * @author: 张建飞
 * @createDate: 2019/12/28
 * @version: 1.0
 */
@CrossOrigin
@Controller
@RequestMapping
public class ImgScanController {

    ResponseData responseData;

    @RequestMapping("scanCarNum")
    @ResponseBody
    public ResponseData ImgScan(String imgUrl) {
        try {
            // 密匙id
            Credential cred = new Credential("AKIDWorfEXrXHm26ysPy71f3MDVYLsAJYpLm", "LQHtFLCvYv2ulMhT0KPEn45qLZYIp6IQ");
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 客户端配置
            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
            // 图片路径
            String params = "{\"ImageUrl\":" + "\"" + imgUrl + "\"" + "}";
            System.out.println(params);
            // 进行扫描
            LicensePlateOCRRequest req = LicensePlateOCRRequest.fromJsonString(params, LicensePlateOCRRequest.class);
            //扫描结果
            LicensePlateOCRResponse resp = client.LicensePlateOCR(req);

            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("carNum", resp);
            System.out.println(LicensePlateOCRRequest.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return responseData;
    }
}
